

#pragma once
#include <iostream>
#include <unordered_map>
#include <functional>
#include <unistd.h>
// 任务模拟

static int taskNUm = 0;
using task_t = std::function<void()>;

class taskManager
{
private:
    std::unordered_map<int, task_t> tasks;

public:
    taskManager(/* args */);
    ~taskManager();

    void insertTask(task_t task)
    {
        tasks[taskNUm++] = task;
    }
    void excute(int number)
    {
        if (tasks.find(number) == tasks.end())
            return;
        tasks[number]();
    }

    int selectTask()
    {
        return rand() % taskNUm;
    }

    
};

static taskManager tm;
void pt()
{
    std::cout << "print" << std::endl;
}
void wk() { std::cout << "worker" << std::endl; }

void rd() { std::cout << "read" << std::endl; };

taskManager::taskManager(/* args */)
{
    insertTask(pt);
    insertTask(wk);
    insertTask(pt);
}

taskManager::~taskManager()
{
}
void worker()
{
    // std::cout << "worker pid: " << ::getpid() << std::endl;
    

    while (true)
    {
        int cmd = 0;
        int n = ::read(0, &cmd, sizeof cmd);
        // std::cout << "read cmd: " << cmd << std::endl;
        if (n == sizeof cmd)
        {
            
            // std::cout << "enter " << std::endl;
            tm.excute(cmd);
        }
        else if (n == 0)
        {
            std::cout << "pid" << ::getpid() << " quit" << std::endl;
            break;
        }

        else
        {
        }
        sleep(3);
    }
}